package com.zy.explore.license.domain.mybatis.entity;

import com.zy.explore.license.infrastructure.base.mybatis.TkBaseEntity;
import com.zy.explore.license.interfaces.frm.ExampleQueryFrm;
import com.zy.explore.license.interfaces.frm.SortFieldFrm;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.data.domain.Sort;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.List;

/**
 * 示例表(表example)ORM实体对象
 *
 * @author 🇨🇳中国印🇨🇳
 * @date 2022年04月17日 01:33:12
 */
@Entity
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "example")
public class TkExampleEntity extends TkBaseEntity<TkExampleEntity, String> {

    /**
     * 主键ID
     */
    @Id
    @Column(insertable = false, name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY, generator = "JDBC")
    private String id;

    /**
     * 姓名
     */
    private String name;

    /**
     * 联系地址
     */
    private String address;
	
	/**
     * Frm 转 Example
     *
     * @param frm 参数对象
     * @return 返回Example对象
     */
    public static Example getExampleForQuery(ExampleQueryFrm frm) {
        if (frm.getPageNum() == null || frm.getPageNum() <= 0) {
            frm.setPageNum(1);
        }
        if (frm.getPageSize() == null || frm.getPageSize() <= 0) {
            frm.setPageSize(10);
        }
        Example example = new Example(TkExampleEntity.class);
        Example.Criteria criteria = example.createCriteria();
        if (frm.getId() != null) {
            criteria.andEqualTo("id", frm.getId());
        }
        if (!StringUtils.isEmpty(frm.getName())) {
            criteria.andLike("name", frm.getName() + "%");
        }
        if (!StringUtils.isEmpty(frm.getAddress())) {
            criteria.andLike("address", frm.getAddress() + "%");
        }
        if (!StringUtils.isEmpty(frm.getCreateTimeBegin())) {
            criteria.andGreaterThanOrEqualTo("createTime", frm.getCreateTimeBegin());
        }
        if (!StringUtils.isEmpty(frm.getCreateTimeEnd())) {
            criteria.andLessThanOrEqualTo("createTime", frm.getCreateTimeEnd());
        }
        if (!StringUtils.isEmpty(frm.getUpdateTimeBegin())) {
            criteria.andGreaterThanOrEqualTo("updateTime", frm.getUpdateTimeBegin());
        }
        if (!StringUtils.isEmpty(frm.getUpdateTimeEnd())) {
            criteria.andLessThanOrEqualTo("updateTime", frm.getUpdateTimeEnd());
        }
        // 排序
        List<SortFieldFrm> sortFields = frm.getSortFields();
        if (!CollectionUtils.isEmpty(sortFields)) {
            sortFields.forEach(sortField -> {
                if (Sort.Direction.ASC.name().equalsIgnoreCase(sortField.getOrder())) {
                    example.orderBy(sortField.getPropName()).asc();
                } else if (Sort.Direction.DESC.name().equalsIgnoreCase(sortField.getOrder())) {
                    example.orderBy(sortField.getPropName()).desc();
                }
            });
        } else {
            // 默认按主键降序排序
            example.orderBy("id").desc();
        }
        return example;
    }

}